package com.example.administrator.u17.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.activity.SearchClasifyActivity;
import com.example.administrator.u17.activity.ZhangJieActivity;
import com.example.administrator.u17.adapter.SearchTopDetailAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.modle.MyDbBean;
import com.example.administrator.u17.modle.SearchTopDetailBean;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Smile on 2016/11/15.
 */

public class HotFragment extends Fragment {
    private ArrayList<SearchTopDetailBean.Comics> mData;
    private SearchTopDetailAdapter mAdapter;
    private PullToRefreshListView mListView;
    private String mTitle;
    private String mArgName;
    private String mTabName;
    private int mArgValue;
    private int mArgCon;
    private int mCurrentPage=1;
    private DbManager mDbManager;
    private String mPath=null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=View.inflate(getActivity(), R.layout.search_detail_item,null);
        mDbManager=x.getDb(new DbManager.DaoConfig());
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage=1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage++;
                loadData();
            }
        });
        //PullToRefreshListView的点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),ZhangJieActivity.class);
                intent.putExtra("id",mData.get(position).comicId);
                startActivity(intent);
            }
        });
    }

    private void setData() {
        mListView.setAdapter(mAdapter);
    }

    private void initData() {
        Bundle bundle=getArguments();
        mTitle = bundle.getString("title");
        mArgName = bundle.getString("argName");
        mTabName = bundle.getString("tabTitle");
        mArgValue = bundle.getInt("argValue");
        mArgCon = bundle.getInt("argCon");
        mData=new ArrayList<>();
        mAdapter=new SearchTopDetailAdapter(getActivity(),mData);
        loadData();
    }
    private void loadData() {
        mPath = NetConfig.SEARCH_TOP_DETAIL_PATH+mArgValue+"&argName="+mArgName+"&argCon="+mArgCon+"&page="+mCurrentPage;
        RequestParams params=new RequestParams(mPath);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                MyDbBean myDb=new MyDbBean();
                myDb.json=result;
                myDb.path=mPath;
                Calendar calendar=Calendar.getInstance();
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                myDb.time=format.format(calendar.getTime());
                try {
                    mDbManager.save(myDb);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                SearchTopDetailBean bean=new Gson().fromJson(result,SearchTopDetailBean.class);
                mData.addAll(bean.data.returnData.comics);
                mAdapter.notifyDataSetChanged();
                mListView.onRefreshComplete();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网络异常", Toast.LENGTH_SHORT).show();
                try {
                    List<MyDbBean> data=mDbManager.selector(MyDbBean.class).where("path","=",mPath).findAll();
                    SearchTopDetailBean bean=new Gson().fromJson(data.get(0).json,SearchTopDetailBean.class);
                    mData.addAll(bean.data.returnData.comics);
                    mAdapter.notifyDataSetChanged();
                    mListView.onRefreshComplete();
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mListView.onRefreshComplete();
            }
        });

    }


    private void initView(View view) {
        mListView= (PullToRefreshListView) view.findViewById(R.id.pl_search_top_detial_activity);

    }
}
